﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Planning.Forms
{
    public partial class infoFormation : Form
    {
        #region Contruct
        private int idFormation;
        private int idMembre;
        private bool inscrit;
       
        public infoFormation(int idFormation, int idMembre) {
            InitializeComponent();
            this.idFormation = idFormation;
            this.idMembre = idMembre;
            DataTable ds = SQL("SELECT * FROM PARTICIPANT WHERE idMembre = " + this.idMembre + " AND idFormation = " + this.idFormation + ";");
            if (ds.Rows.Count == 0) { 
                this.inscrit = false;
            } else { 
                this.inscrit = true;
                button1.Text = "Se désinscrire";
            }
            
            getInfos();
        }
        #endregion

        #region SQL
        public DataTable SQL(string req) {
            DataTable retour = new DataTable();
            using (SqlConnection connexion = new SqlConnection("user id=" + Properties.Settings.Default.userBase + "; " +
                    "password=" + Properties.Settings.Default.pwdBase + "; " +
                    "server=" + Properties.Settings.Default.adrBase + "; " +
                    "database=" + Properties.Settings.Default.nomBase + "; " +
                    "connection timeout=30"))
            {
                try {
                    using (SqlCommand cmd = connexion.CreateCommand()) {
                        cmd.CommandText = req;
                        cmd.Connection.Open();
                        retour.Load(cmd.ExecuteReader());
                    }
                }
                catch (SqlException ex) {
                    if (Properties.Settings.Default.erreurSql) {
                        MessageBox.Show("Erreur de connexion à la base de données. \n\n" + ex, "Erreur !", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        this.Close();
                    } else {
                        MessageBox.Show("Erreur de connexion à la base de données.", "Erreur !", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        this.Close();
                    }
                } finally {
                    connexion.Close();
                }
            }
            return retour;
        }

        #endregion

        #region Getters
        public void getInfos() {
            DataTable res = new DataTable();
            DataTable res2 = new DataTable();
            res = SQL("SELECT libelleFormation, descrFormation, dureeFormation, dateFormation, nbPlacesFormation FROM FORMATION WHERE idFormation = " + this.idFormation + ";");
            res2 = SQL("SELECT COUNT(idMembre) as total FROM PARTICIPANT WHERE idFormation = " + this.idFormation + ";");

            libelleFormationText.Text = res.Rows[0]["libelleFormation"].ToString();
            descrFormation.Text = res.Rows[0]["descrFormation"].ToString();
            dureeFormationText.Text = res.Rows[0]["dureeFormation"].ToString() + " minutes";

            dateFormation.Value = Convert.ToDateTime(res.Rows[0]["dateFormation"].ToString());
            placesLibresFormation.Text = (Convert.ToInt32(res.Rows[0]["nbPlacesFormation"]) - Convert.ToInt32(res2.Rows[0]["total"])).ToString();
        }
        #endregion

        #region clics
        private void button1_Click(object sender, EventArgs e) {
            using (SqlConnection connexion = new SqlConnection("user id=" + Properties.Settings.Default.userBase + "; " +
                    "password=" + Properties.Settings.Default.pwdBase + "; " +
                    "server=" + Properties.Settings.Default.adrBase + "; " +
                    "database=" + Properties.Settings.Default.nomBase + "; " +
                    "connection timeout=30"))
            {
                try {
                    using (SqlCommand cmd = connexion.CreateCommand()) {
                        if (this.inscrit) {
                            cmd.CommandText = "INSERT INTO PARTICIPANT VALUES (@idMembre, @idFormation);";
                        }
                        else {
                            cmd.CommandText = "DELETE FROM PARTICIPANT WHERE idMembre = @idMembre AND idFormation = @idFormation;";
                        }
                        cmd.Parameters.AddWithValue("@idMembre", this.idMembre);
                        cmd.Parameters.AddWithValue("@idFormation", this.idFormation);
                        cmd.Connection.Open();
                        if (cmd.ExecuteNonQuery() != 1) {
                            if (this.inscrit) {
                                MessageBox.Show("Erreur lors de l'inscription");
                            } else {
                                MessageBox.Show("Erreur lors de la désinscription");
                            }
                        }
                    }
                }
                catch (SqlException ex) {
                    if (Properties.Settings.Default.erreurSql) {
                        MessageBox.Show("Erreur de connexion à la base de données. \n\n" + ex, "Erreur !", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    } else {
                        MessageBox.Show("Erreur de connexion à la base de données.", "Erreur !", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                } finally {
                    connexion.Close();
                }
            }
        }
        #endregion
    }
}
